package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.R;
import com.example.xymarket.httpUtil.AsyncImageLoader;
import com.example.xymarket.httpUtil.GoodsService;
import com.example.xymarket.po.Goods;
import com.example.xymarket.common.MyHandler;
import com.example.xymarket.common.MyUrl;

import java.util.List;

public class GoodsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);
        int i=(int)getIntent().getIntExtra("id",0);
        MyHandler Myhandler=new MyHandler(this,thisHandler);
        GoodsService goodsService=new GoodsService(Myhandler);
        StatusBar.setStatusBarColor(this,StatusBar.yellow);
        goodsService.getGoodsById(i);
    }

    @SuppressLint("HandlerLeak")
    public final Handler thisHandler = new Handler()
    {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==101) {
                List<Goods> goodsList=(List<Goods>)msg.obj;
                Goods goods=goodsList.get(0);
                init(goods);
            }
            if(msg.what==-101)
            {
                Toast.makeText(getApplicationContext(),"请求失败",Toast.LENGTH_LONG).show();

            }
        }
    };

    public void init(Goods goods)
    {
        TextView goods_name=(TextView) findViewById(R.id.goods_name);
        goods_name.setText(goods.getTitle());
        TextView goods_price=(TextView) findViewById(R.id.goods_price);
        goods_price.setText(String.valueOf(goods.getPrice() / 100) + "." + String.valueOf(goods.getPrice() % 100));
        TextView goods_sold=(TextView) findViewById(R.id.goods_sold);
        goods_sold.setText("月销：" + String.valueOf(goods.getSoldQty()));
        ImageView goods_pic=(ImageView) findViewById(R.id.goods_pic);
        AsyncImageLoader asyncImageLoader=new AsyncImageLoader();
        asyncImageLoader.loadImage(MyUrl.ImagesUrl + goods.getImage(),goods_pic);
        TextView dis=(TextView) findViewById(R.id.goods_disp);
        dis.setText(goods.getGoodsDesc());
    }

}